﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using weikebuluo.DAL;
using weikebuluo.Common;
using weikebuluo.Model;

namespace weikebuluo.web.admin.user 
{
    public partial class useradd : weikeweixin.web.admin.cs.rootbase
    {
        a_user_dal userdal = new a_user_dal();
        protected a_user_model usermodel = new a_user_model();


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["user_id"]))
            {

                usermodel = userdal.GetModel(int.Parse(Request.QueryString["user_id"]));
            }
            switch (Request.QueryString["action"])
            {
                case "adduser":  //新增用户
                    addUser();
                    break;

            }

        }


        private void addUser()
        {
          

            string User_Name = Request.Form["name"].Trim();
            string User_Phone = Request.Form["phone"].Trim();
            string User_Email = Request.Form["email"].Trim();
            string User_Pwd = Request.Form["pwd"].Trim();
            string User_QQ = Request.Form["qq"].Trim();



            System.Data.DataSet dsXml = new System.Data.DataSet();
            //dsXml.ReadXml(Server.MapPath("/Config/Xml/InviteCode.xml"));
            //string InviteCode = dsXml.Tables[0].Rows[0][0].ToString().Trim();
            dsXml.Clear();

            if (!PageValidate.IsValidInput(ref User_Name, true))
            {
                Response.Write("请输入正确的用户名");
            }
            else if (userdal.GetList("user_name='" + User_Name + "'").Tables[0].Rows.Count > 0)
            {
                Response.Write("用户名已重复");
            }
            else if (!PageValidate.IsNumber(User_Phone) || User_Phone.Length != 11)
            {
                Response.Write("请输入正确的手机号码");
            }
            else if (userdal.GetList("user_phone='" + User_Phone + "'").Tables[0].Rows.Count > 0)
            {
                Response.Write("手机号码已注册");
            }
            else if (!PageValidate.IsEmail(User_Email))
            {
                Response.Write("请输入正确的邮箱");
            }
            else if (userdal.GetList("user_email='" + User_Email + "'").Tables[0].Rows.Count > 0)
            {
                Response.Write("邮箱已注册");
            }


            else
            {

                if (string.IsNullOrEmpty(User_Pwd))
                {
                    User_Pwd = "123456";

                }

                usermodel.user_addtime = DateTime.Now;             
                usermodel.user_email = User_Email;
                usermodel.user_name = User_Name;
                usermodel.user_phone = User_Phone;             
                usermodel.user_qq = User_QQ;
                usermodel.user_psw = MD5Provider.GetMd5(User_Pwd);
                usermodel.user_lasttime = DateTime.Now;
                usermodel.user_level = 1;
              
               
                    
           
                string User_ID = userdal.Add(usermodel).ToString();
                if (int.Parse(User_ID) > 0)
                {
                    Response.Write("true");
                }
                else
                {
                    Response.Write("未知错误");
                }

               
            }
            Response.End();
        }
    }
}